//
//  DHHomeMagazineCell.m
//  Dresshere
//
//  Created by ZhangPeng on 2018/4/14.
//  Copyright © 2018年 张朋. All rights reserved.
//

#import "DHHomeMagazineCell.h"
#import "DHMagazine_LargeCell.h"
#import "DHMagazine_NormalCell.h"
#import "DHNewsDetailController.h"

@interface DHHomeMagazineCell()<UITableViewDelegate, UITableViewDataSource>

@property (strong, nonatomic) NSArray *itemArr; //杂志数组

@end

@implementation DHHomeMagazineCell

- (void)awakeFromNib {
    [super awakeFromNib];
    self.moreButton.layer.cornerRadius = self.moreButton.height / 2;
    
    WeakSelf(weakSelf)
    [self.moreButton bk_addEventHandler:^(id sender) {
        [weakSelf.viewController.tabBarController setSelectedIndex:4];
    } forControlEvents:UIControlEventTouchUpInside];
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.tableView registerNib:[UINib nibWithNibName:@"DHMagazine_LargeCell" bundle:nil] forCellReuseIdentifier:kDHMagazine_LargeCell];
    [self.tableView registerNib:[UINib nibWithNibName:@"DHMagazine_NormalCell" bundle:nil] forCellReuseIdentifier:kDHMagazine_NormalCell];
}

- (NSArray *)itemArr{
    if (!_itemArr) {
        _itemArr = [NSArray array];
    }
    return _itemArr;
}

- (void)updateCellWithArray:(NSArray *)arr{
    self.itemArr = arr;
    [self.tableView reloadData];
}


#pragma mark - UITableViewDelegate and UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.itemArr.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSDictionary *model = [self.itemArr objectAtIndex:indexPath.row];
    if ([[model objectForKey:@"layout"] isEqualToString:@"大矩形"]) {
        return 210;
    }else{
        return 100;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSDictionary *model = [self.itemArr objectAtIndex:indexPath.row];
    if ([[model objectForKey:@"layout"] isEqualToString:@"大矩形"]) {
        DHMagazine_LargeCell *cell = [tableView dequeueReusableCellWithIdentifier:kDHMagazine_LargeCell forIndexPath:indexPath];
        [cell updateCell:model];
        return cell;
    }else{
        DHMagazine_NormalCell *cell = [tableView dequeueReusableCellWithIdentifier:kDHMagazine_NormalCell forIndexPath:indexPath];
        [cell updateCell:model];
        return cell;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSDictionary *model = [self.itemArr objectAtIndex:indexPath.row];
    DHNewsDetailController *vc = [[DHNewsDetailController alloc] initWithArticleID:[model objectForKey:@"_id"]];
    vc.hidesBottomBarWhenPushed = YES;
    [self.viewController.navigationController pushViewController:vc animated:YES];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
